layui.config({
    base: '../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['element','jquery','layer','form','table','laypage','index'], function(){
    var element = layui.element;
    var $ = layui.jquery;
    var layer = layui.layer; //独立版的layer无需执行这一句
    var role= $.cookie('role');
    var adminId=$.cookie('adminId');
    //角色判断给予权限
    if(role=='admin'){
        $("[name=iframe]").attr('src', '/view/statistics.html');
    }
    $.cookie('role',role);
    if (role=="user"){
        $("#topNav").css("display","none")
        $("#MyInfo").css("display","none")
    }
    /*判断用户是否登录*/
    $.ajax({
        url:"/login/myInfo",
        type:"post",
        success:function (result) {
            if(result.data==null){
                window.location.href="login.html";
            }
        }
    });
    var username=$.cookie("username");

    $("#username").text(username);
    $("#exit").click(function () {
        layer.confirm('确定要退出吗？', {
            btn: ['是', '否']
        }, function(index, layero){
            $.ajax({
                url:"/login/loginOut",
                type:"get",
                success:function (result) {
                    if(result.code==0){
                        layer.msg("操作成功！");
                        $.cookie('nickname',null);
                        window.location.href="login.html";
                    }else {
                        layer.msg(result.message);
                    }
                }
            });
        }, function(index){
        });

    });
   /**
    * 功能描述: <br>
    * 〈查看我的资料〉
    * @Author: wgh
    * @Date: 2019/7/16 0016 14:55
    */
    //点击查看基本资料并修改
    $("#MyInfo").click(function () {
        layer.open({
            type: 2
            ,area:["900px","680px"]
            ,title: "我的资料"
            /*,shadeClose: true
            ,shade: false*/
            ,content: 'admin/myInfo.html'
            ,yes: function( index,layero){
                layer.close(index);//需要手动关闭窗口
                //监听提交
            }

        });
    })

    // 清除缓存
    $("#clearCache").click(function() {
        window.sessionStorage.clear();
        window.localStorage.clear();
        var index = layer.msg('清除缓存中，请稍候', {
            icon : 16,
            time : false,
            shade : 0.8
        });
        setTimeout(function() {
            layer.close(index);
            layer.msg("缓存清除成功！");
        }, 1000);
    });
// 息屏
    $("#screen").click(function() {
        layer.open({
            type: 1,
            icon : 20,
            time : false,
            shade : 0.9,
            btn: ["返回系统"],
            title:["&nbsp;","height:0px;"], //不设置标题
            closeBtn : 0
        });
    });
    //点击修改密码
    $("#updatePwd").click(function () {
        layer.open({
            type: 2,
            title: "修改密码",
            shadeClose: false,
            shade: false,
            area: ["480px","360px"],
            content: "admin/updatePwd.html",
            yes: function( index,layero){
                layer.close(index);//需要手动关闭窗口
                //监听提交
            }
        });
    })


    //加载菜单信息
    $.ajax({
        url:"/login/menus",
        type:"get",
        success:function (result) {
            if(result.code==0){
                var menus=result.data;
                var data=getData(menus);
                eachTree(data);
                element.init();
                // $("[name=iframe]").attr('src', '/view/goodsOrder/goodsOrder.html');
            }else {
                layer.msg(result.message);
            }
        }
    });

});

function fnW(str) {
    var num;
    str >= 10 ? num = str : num = "0" + str;
    return num;
}
//获取当前时间
var timer = setInterval(function () {
    var date = new Date();
    var year = date.getFullYear(); //当前年份
    var month = date.getMonth(); //当前月份
    var data = date.getDate(); //天
    var hours = date.getHours(); //小时
    var minute = date.getMinutes(); //分
    var second = date.getSeconds(); //秒
    var day = date.getDay(); //获取当前星期几
    var week;
    if (day==1){
        week="一";
    } else if (day == 2) {
        week="二";
    }else if (day == 3) {
        week="三";
    }else if (day == 4) {
        week="四";
    }else if (day == 5) {
        week="五";
    }else if (day == 6) {
        week="六";
    }else if (day == 7) {
        week="日";
    }
    $('#time').html('<span>' + year + '年' + (month + 1) + '月' + data + '日</span><span>星期' + week + '</span>'+fnW(hours) + ":" + fnW(minute) + ":" + fnW(second));

}, 1000)



/**
 * 获取子菜单
 * @param menus
 * @param obj
 */
function getTree(menus,obj) {
    var menus2=menus;
    var chlist=[];
    for (var i=0;i<menus2.length;i++){
        var menu=menus2[i];
        if(menu.parentId==obj.id){
            var menuTree={
                title:menu.name,
                id:menu.id,
                url:menu.url,
                spread:true
            };
            getTree(menus,menuTree);
            chlist.push(menuTree);
        }
    }
    obj.children=chlist;
}

/**
 * 获得菜单树数据
 * @param menus
 * @returns {Array}
 */
function getData(menus) {
    var lastMenus=[];
    for (var i=0;i<menus.length;i++){
        var menu=menus[i];
        if(menu.parentId==null){
            var menuTree={
                title:menu.name,
                id:menu.id,
                url:menu.url,
                spread:true
            }
            lastMenus.push(menuTree);
        }
    }
    for (var i=0;i<lastMenus.length;i++){
        getTree(menus,lastMenus[i]);
    }
    return lastMenus;
}
//遍历菜单树数据并展示到页面
function eachTree(data) {
    for(var i=0;i<data.length;i++){
        var obj=data[i];
        //li标签
        var li=$("<li>",{
            "class":"layui-nav-item layui-nav-itemed"
        });
        var a=$("<a>",{
            "href":obj.url||"javascript:;",
            "text":obj.title
        });
        var dl=$("<dl>",{
            "class":"layui-nav-child"
        });

        if( obj.children.length > 0 ){
            $.each( obj.children, function(k, v){
                var dd=$("<dd>");
                var a2=$("<a>",{
                    "lay-href":v.url,
                    "target":"iframe",
                    "text":v.title
                });

                dd.append(a2);
                dl.append(dd);
            });
        }
        li.append(a);
        li.append(dl);
        $("#LAY-system-side-menu").append(li)
    }
}



